//
//  FiveViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/25.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "MeViewController.h"

#import "MeTableViewCell.h"
#import "Common.h"
#import "MeView.h"
#import "MeNameView.h"
#import "CommonView.h"

#import "SetViewController.h"
#import "MyPushViewController.h"
#import "MyHelpViewController.h"
#import "MyValuesViewController.h"
#import "MeChangeInfoViewController.h"
#import "MyWalletViewController.h"
#import "CollectionViewController.h"

#import "BGMUser.h"
#import "BGMHTTP.h"
#import "UIImageView+WebCache.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)UITableView *meTableView;
@property (nonatomic, strong)UIImageView *bgheadImage;
@property (nonatomic, strong)UIImageView *headImage;
@property (nonatomic, strong)MeNameView *mename;

@end

@implementation MeViewController
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getUserInfo];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topBackView.hidden = NO;
    
      self.bgheadImage = [[UIImageView alloc]initWithFrame:CGRectMake(-70, -50, self.topBackView.frame.size.width+140, self.topBackView.frame.size.height+100)];
    [self.bgheadImage setImage:[UIImage imageNamed:@"headImage"]];
    self.bgheadImage.alpha = 0.1;
    [self.topBackView addSubview:self.bgheadImage];
    
    self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENW/4, SCREENW/4)];
    [self.headImage setImage:[UIImage imageNamed:@"headImage"]];
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = SCREENW/4/2;
    self.headImage.center = CGPointMake(SCREENW/2, self.topBackView.frame.size.height/2-30);
    [self.view addSubview:self.headImage];
    
    self.headImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickMY)];
    [self.headImage addGestureRecognizer:tap1];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *openSet = [userDefaults objectForKey:@"openSet"];
    if ([openSet isEqualToString:@"YES"]) {
        self.array = @[@"靠谱值",@"我的钱包",@"设置"];
    }else{
        self.array = @[@"靠谱值",@"我的关注",@"设置"];
    }

   
    UIView *bgmeView = [[UIView alloc]initWithFrame:CGRectMake(15, self.topBackView.frame.size.height-50, SCREENW-30, 100)];
    bgmeView.backgroundColor = [UIColor whiteColor];
    bgmeView.layer.cornerRadius = 10;
    bgmeView.layer.masksToBounds = YES;
    [self.view addSubview:bgmeView];
    NSArray *arr = @[@"发布",@"我帮的忙",@"帮转"];
    float  w = bgmeView.frame.size.width/3;
    for (int i = 0; i<3; i++) {
        MeView *meView = [[MeView alloc]initWithFrame:CGRectMake(w*i, 0,w, bgmeView.frame.size.height)];
        meView.label1.text = [arr objectAtIndex:i];
        meView.tag = 100+i;
        [bgmeView addSubview:meView];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
        btn.backgroundColor =[ UIColor clearColor];
        btn.center = meView.center;
        btn.tag = 200+i;
        [bgmeView addSubview:btn];
        [btn addTarget:self action:@selector(gotoNextVC:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.mename = [[MeNameView alloc]initWithFrame:CGRectMake(0, self.headImage.center.y+self.headImage.frame.size.height/2, SCREENW, self.topBackView.frame.size.height-50-(self.headImage.center.y+self.headImage.frame.size.height/2))];
    self.mename.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap =[[ UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickMY)];
    [self.mename addGestureRecognizer:tap];
    [self.view addSubview:self.mename];
    
    
    self.meTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, bgmeView.frame.origin.y+110+10, SCREENW, SCREENH-(bgmeView.frame.origin.y+110)-10)];
    self.meTableView.backgroundColor = [UIColor clearColor];
    self.meTableView.delegate = self;
    self.meTableView.dataSource = self;
    self.meTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.meTableView];
    
}



////行业标签列表
//- (void)getUserInstrysList{
//    [BGMHTTP requestWithMethodName:@"login/getUserIndustrysList" parameters:@{@"key":@"values"} completion:^(BOOL success, NSError *error, NSDictionary *result) {
//        self.userInstrys = [result objectForKey:@"data"];
//    }];
//}
////行业标签列表
//- (void)getUserLable{
//    [BGMHTTP requestWithMethodName:@"login/getUserLable" parameters:@{@"key":@"values"} completion:^(BOOL success, NSError *error, NSDictionary *result) {
//        self.usertitle = [result objectForKey:@"data"];
//    }];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
 
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"MeTableViewCell";
    MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell ==nil) {
        cell = [[MeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell.iconBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"me_%d",(int)indexPath.row+1]] forState:UIControlStateNormal];
    cell.contentLabel.text =[self.array objectAtIndex:indexPath.row];
    
  
    if (indexPath.row==0){
        BGMUser *user = [BGMUser shareInstance];
        cell.rightBtn.hidden = NO;
        cell.rightLabel.hidden = NO;
        cell.rightLabel.text = user.spectralValue;
        cell.rightLabel.textColor = bgmmainColor;
        cell.rightLabel.font = [UIFont systemFontOfSize:20];
        cell.rightLabel.frame =CGRectMake(120, 0, SCREENW-160, 50);
        [cell.iconBtn setImage:[UIImage imageNamed:@"me_1"] forState:UIControlStateNormal];
 
    }
//    else if (indexPath.row ==1) {
//        cell.rightBtn.hidden = YES;
//        cell.rightLabel.hidden = NO;
//        cell.rightLabel.text = @"即将到来";
//        cell.rightLabel.textColor = bgmgrayColor;
//        cell.rightLabel.font = [UIFont systemFontOfSize:12];
//        cell.rightLabel.frame = CGRectMake(120, 0, SCREENW-140, 50);


//    }
else{

        cell.rightBtn.hidden = NO;
        cell.rightLabel.hidden = YES;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = (int)indexPath.row;
    switch (row) {
        case 0:
        {
            MyValuesViewController *vc = [[MyValuesViewController alloc]init];
            BGMUser *user = [BGMUser shareInstance];
            vc.valueStr = user.spectralValue;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *openSet = [userDefaults objectForKey:@"openSet"];
            if ([openSet isEqualToString:@"YES"]) {
                MyWalletViewController *vc = [[MyWalletViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                CollectionViewController *vc = [[CollectionViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
          
        }
            break;
        case 2:
        {
            SetViewController *vc = [[SetViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {

        }
            break;
            
        default:
            break;
    }
}

- (void)getUserInfo{
    BGMUser *user = [BGMUser shareInstance];
    NSString  *userToken = user.userToken;
    NSString  *userID = user.userId;
    [BGMHTTP requestWithMethodName:@"users/getUsersPageInfo" parameters:@{@"userToken":userToken,@"userId":userID} completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
             [self saveInfomation:result];
        }
    }];
}

- (void)saveInfomation:(NSDictionary *)dictory{
    
    NSDictionary *dic = [dictory objectForKey:@"data"];
    BGMUser *user = [BGMUser shareInstance];
    user.birthday = [NSString stringWithFormat:@"%@",[dic objectForKey:@"birthday"]];
    user.city = [NSString stringWithFormat:@"%@",[dic objectForKey:@"city"]];
    user.gender = [NSString stringWithFormat:@"%@",[dic objectForKey:@"gender"]];
    user.headImage = [NSString stringWithFormat:@"%@",[dic objectForKey:@"headImage"]];
    user.imageArray = [dic objectForKey:@"imageios"];
    user.industry = [NSString stringWithFormat:@"%@",[dic objectForKey:@"industry"]];
    user.name = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
    user.personalLabelArray =[dic objectForKey:@"personalLabel"];
    user.phone = [NSString stringWithFormat:@"%@",[dic objectForKey:@"phone"]];
    user.spectralValue = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spectralValue"]];
    user.userHelpSize = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userHelpSize"]];
    user.userPublishAcceptancesSize = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userPublishAcceptancesSize"]];
    user.userPublishSize = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userPublishSize"]];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSDictionary *userDic = [ud objectForKey:usermessage];
    NSMutableDictionary *newDic = [[NSMutableDictionary alloc]initWithDictionary:userDic];
    [newDic setValue:user.headImage forKey:@"headImage"];
    [ud setObject:newDic forKey:usermessage];
    [ud synchronize];
   
    [self reloadMEData];

}

- (void)reloadMEData{
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.meTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    BGMUser *user = [BGMUser shareInstance];
    MeView *meView1 = (MeView *)[self.view viewWithTag:100];
    meView1.countLabel.text = user.userPublishSize;
    MeView *meView2 = (MeView *)[self.view viewWithTag:101];
    meView2.countLabel.text = user.userPublishAcceptancesSize;
    MeView *meView3 = (MeView *)[self.view viewWithTag:102];
    meView3.countLabel.text = user.userHelpSize;
    
    self.mename.nameLabel.text = user.name;
    [self.bgheadImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",user.headImage]] placeholderImage:[UIImage imageNamed:@"headImage"]];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",user.headImage]] placeholderImage:[UIImage imageNamed:@"headImage"]];
}

#pragma mark ---- 我的 -----
- (void)gotoNextVC:(UIButton *)btn{
    int tag = (int)btn.tag;
    switch (tag) {
        case 200:  //我的发布
        {
            MyPushViewController *vc = [[MyPushViewController alloc]init];
            vc.isOther = NO;
            [self.navigationController pushViewController:vc animated:YES];
           
        }
            break;
        case 201:  //我帮的忙
        {
            MyHelpViewController *vc = [[MyHelpViewController alloc]init];
            vc.title_Str = @"我帮的忙";
            vc.isAccpet = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        case 202:  //我的帮转
        {
            MyHelpViewController *vc = [[MyHelpViewController alloc]init];
            vc.title_Str = @"我的帮转";
            vc.isForward = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
            
            
        default:
            break;
    }
}
#pragma mark 修改个人资料
- (void)clickMY{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSArray * usertitle = [userDefault objectForKey:@"usertitle"];
    NSArray * userInstrys = [userDefault objectForKey:@"userInstrys"];

    MeChangeInfoViewController *vc = [[MeChangeInfoViewController alloc]init];
    vc.userTitles = usertitle;
    vc.userIntrys = userInstrys;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
